package com.ohs.bo.action;

import java.util.ArrayList;
import java.util.List;

import com.ohs.bll.service.AccountService;
import com.ohs.bll.service.RoleService;
import com.ohs.dal.model.Account;
import com.ohs.dal.model.Role;
import com.ohs.fo.action.AbstractAction;
import com.ohs.fo.form.RoleInfo;
import com.ohs.fo.form.UserInfo;

public class AccountAction extends AbstractAction {

	private String username;

	private String password;
	
	private String rePassword;

	private String name;

	private String age;
	
	private String title;
	
	private boolean addNew;
	
	private long role;
	
	private List<RoleInfo> roleList;
	
	private RoleService roleService;

	private AccountService accountService;

	private List<UserInfo> results;
	
	public String list() throws Exception {
		List<Role> roles = roleService.list();
		roleList = new ArrayList<RoleInfo>();
		for (Role role : roles) {
			RoleInfo roleInfo = new RoleInfo();
			roleInfo.setRoleId(role.getRoleId());
			roleInfo.setName(role.getName());
			roleInfo.setDescription(role.getDescription());
			roleList.add(roleInfo);
		}
		results = new ArrayList<UserInfo>();
		List<Account> accounts = accountService.list();
		if (accounts != null) {
			for (Account account : accounts) {
				UserInfo user = new UserInfo();
				user.setUsername(account.getUsername());
				if (account.getAge() != null) {
					user.setAge(account.getAge());
				}
				user.setName(account.getName());
				user.setTitle("");
				results.add(user);
			}
		}
		return "success";
	}
	
	public String updateAccount() throws Exception {
		boolean result = accountService.update(username, password, name, age, title, role, addNew);
		if (result) {
			return "success";
		} else {
			addActionMessage(getText("admin.account.user.existed"));
			return "input";
		}
	}

	public String editAccount() throws Exception {
		Account account = accountService.get(username);
		username = account.getUsername();
		password = account.getPassword();
		rePassword = account.getPassword();
		name = account.getName();
		if (account.getAge() != null) {
			age = account.getAge().toString();
		} else {
			age = "";
		}
		role = account.getRole().getRoleId();
		return "success";
	}
	
	public String deleteAccount() throws Exception {
		accountService.delete(username);
		return "success";
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public long getRole() {
		return role;
	}

	public void setRole(long role) {
		this.role = role;
	}

	public List<RoleInfo> getRoleList() {
		return roleList;
	}

	public void setRoleList(List<RoleInfo> roleList) {
		this.roleList = roleList;
	}

	public RoleService getRoleService() {
		return roleService;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	public String getRePassword() {
		return rePassword;
	}

	public void setRePassword(String rePassword) {
		this.rePassword = rePassword;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getAge() {
		return age;
	}

	public void setAge(String age) {
		this.age = age;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public AccountService getAccountService() {
		return accountService;
	}

	public void setAccountService(AccountService accountService) {
		this.accountService = accountService;
	}

	public List<UserInfo> getResults() {
		return results;
	}

	public void setResults(List<UserInfo> results) {
		this.results = results;
	}

	public boolean isAddNew() {
		return addNew;
	}

	public void setAddNew(boolean addNew) {
		this.addNew = addNew;
	}

}